package com.shensi.common.core.domain;

import com.shensi.common.core.constant.HttpStatus;

/**
 * @date 2023/9/8
 */
public class ResultData<T> {


    public static final String SUCCESS_MSG = "操作成功";
    public static final int SUCCESS_CODE = HttpStatus.SUCCESS;
    public static final int DEFAULT_ERROR_CODE = HttpStatus.ERROR;

    private String msg;
    private int code;
    private T data;

    public ResultData() {
    }

    /**
     * 成功时候的调用
     *
     * @return
     */
    public static <T> ResultData<T> success(T data) {
        ResultData<T> r = new ResultData<>();
        r.code = SUCCESS_CODE;
        r.msg = SUCCESS_MSG;
        r.data = data;
        return r;
    }

    /**
     * 成功，不需要传入参数
     *
     * @return
     */
    @SuppressWarnings("unchecked")
    public static ResultData success() {
        return success(null);
    }

    /**
     * 失败时候的调用
     *
     * @return
     */
    public static ResultData error(String msg) {
        return error(DEFAULT_ERROR_CODE, msg);
    }

    /**
     * 失败时候的调用
     *
     * @return
     */
    public static ResultData error(int code, String msg) {
        return error(code, msg, null);
    }

    /**
     * 失败时候的调用,扩展消息参数
     *
     * @return
     */
    public static <T> ResultData<T> error(int code, String msg, T data) {
        ResultData<T> r = new ResultData<>();
        r.code = code;
        r.msg = msg;
        r.data = data;
        return r;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
